using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveComp : BaseComp
{

    private Rigidbody2D _rigidbody2D;
    // Start is called before the first frame update
    protected override void Awake()
    {
        base.Awake();
        _rigidbody2D = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (_correctTime < 0)
        {
            if (!Map.Instance.IsCanWalk(this.transform.localPosition)) {
                _rigidbody2D.velocity = Vector2.zero;
                _cachePos = this.transform.localPosition;
                _correctPos.x = _cachePos.x - _cacheDir.x / 2;
                _correctPos.y = _cachePos.y - _cacheDir.y / 2;
                _correctPos.z = 0;
                _correctTime = 0;
            }
            else
            {
                DetectPosChange();
            }
        }
        else
        {
            if (_correctTime < _correctTimeMax)
            {
                _correctTime += Time.deltaTime;
                this.transform.localPosition = Vector3.MoveTowards(_cachePos, _correctPos, _correctTime/ _correctTimeMax);
            }
            else
            {
                _correctTime = -1;
                this.transform.localPosition = _correctPos;
            }
        }
    }

    private const float _correctTimeMax = 0.5f;
    private float _correctTime = -1;
    private Vector3 _correctPos = new Vector3();
    private Vector3 _cachePos;
    private bool IsCorrectingPos()
    {
        return _correctTime >= 0;
    }
    public void Move(AniDir dir)
    {
        //if (IsCorrectingPos())
         //   return;
        MoveInternal(dir);
    }

    private int _cacheIndex;
    private Vector2 _cacheDir = new Vector2();
    private void MoveInternal(AniDir aniDir)
    {
        _cacheDir.x = 0;
        _cacheDir.y = 0;
        switch (aniDir)
        {
            case AniDir.Up:
                entity.animationComp.Play(AniType.Walk, AniDir.Up);
                _cacheDir.y = MapMix.GridYL;
                break;
            case AniDir.Down:
                entity.animationComp.Play(AniType.Walk, AniDir.Down);
                _cacheDir.y = -MapMix.GridYL;
                break;
            case AniDir.Left:
                entity.animationComp.Play(AniType.Walk, AniDir.Left);
                _cacheDir.x = -MapMix.GridXL;
                break;
            case AniDir.Right:
                entity.animationComp.Play(AniType.Walk, AniDir.Right);
                _cacheDir.x = MapMix.GridXL;
                break;
            default:
                entity.animationComp.Play(AniType.Idle, AniDir.None);
                break;
        }

        _cacheDir.Normalize();
        _rigidbody2D.velocity = 3 * _cacheDir;//speed
    }

    private int _curIndex;
    private void DetectPosChange()
    {
        _curIndex = MapMix.GetIndex(this.transform.localPosition);
        if (_cacheIndex != _curIndex)
        {
            _cacheIndex = _curIndex;
            Facade.Instance.SendEvent(GameEvent.Move.PosChange, null);
        }
    }

}
